package com.lw.jdbc.dao;

import com.lw.jdbc.domain.User;
import com.lw.jdbc.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoimpl  implements UserDao{

    public void add(User user) throws SQLException {
        //1.创建连接池
        //2.创建连接
        Connection connection = JDBCUtils.getConnection();       //通过 JDBCUtils获取数据库连接
        //3.创建语句
        String  sql="insert into tb_user (username ,password,adress)  values(?,?,?)";   ////
        //4.执行语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//通过connection对象获取PreparedStatement对象
        preparedStatement.setString(1,user.getUsername());
        preparedStatement.setString(2,user.getPassword());
        preparedStatement.setString(3,user.getAdress());   ///将具体的值 写到sql语句中
        preparedStatement.executeUpdate();
        //5.释放资源
        JDBCUtils.release(connection,preparedStatement,null);
    }
    public void delete(Integer id) throws SQLException {
        //1.创建连接池
        //2.创建连接
        Connection connection = JDBCUtils.getConnection();   //获取数据库的链接
        //3.创建语句
        String  sql="delete from tb_user where id=?";
        //4.执行语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//通过connection对象获取PreparedStatement对象
        preparedStatement.setInt(1,id);
        preparedStatement.executeUpdate(); //执行sql语句
        //5.释放资源
        JDBCUtils.release(connection,preparedStatement,null);
    }
    public void update(User user) throws SQLException {
        //1.创建连接池
        //2.创建连接
        Connection connection = JDBCUtils.getConnection(); //获取数据库的链接
        //3.创建语句
        String sql="update tb_user set username=?,password=?,adress=? where id=?";
        //4.执行语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//通过connection对象获取PreparedStatement对象
        preparedStatement.setString(1,user.getUsername());
        preparedStatement.setString(2,user.getPassword());
        preparedStatement.setString(3,user.getAdress());
        preparedStatement.setInt(4,user.getId());
        preparedStatement.executeUpdate();
        //5.释放资源
        JDBCUtils.release(connection,preparedStatement,null);
    }
    public List<User> find() throws SQLException {
        //1.创建连接池
        //2.创建连接
        Connection connection = JDBCUtils.getConnection(); //获取数据库的链接
        //3.创建语句
        String sql="select * from tb_user";//查询所有的数据
        //4.执行语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//通过connection对象获取PreparedStatement对象
        ResultSet resultSet = preparedStatement.executeQuery();  //执行sql语句
        /*  需要把数据进行封装      */
        List<User> list = JDBCUtils.getList(resultSet, User.class);
       /* List<User> list = JDBCUtils.getList(resultSet);*/

        //5.释放资源
        JDBCUtils.release(connection,preparedStatement,resultSet);
        return list;
    }
    public User findById(Integer id) throws SQLException {  ///根据id查询
        //1.注册驱动
        //2.创建连接
        Connection connection = JDBCUtils.getConnection(); //获取数据库的链接
        //3.创建语句
        String sql="select * from tb_user where id=?";  //根据id查询
        //4.执行语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//通过connection对象获取PreparedStatement对象
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();  //执行sql语句
        /*  需要把数据进行封装      */
        List<User> list = JDBCUtils.getList(resultSet);
        //5.释放资源
        JDBCUtils.release(connection,preparedStatement,resultSet);
        return list.get(0);

    }


}

